package indi.gtl.superbrain.tools;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Databasehelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "login_tb";
	private static final String DATABASE_TABLE = "tb_user";
	private static final String D_NAME = "name";
	private static final String D_PASS = "psw";
	private static final String D_IMEI = "imei";
	private static final String D_HEAD = "head";
	private static final String D_SPEED = "speed";
	private static final String D_MEMORY = "memory";
	private static final String D_ACCURACY = "accuracy";
	private static final String D_JUDGEMENT = "judgement";
	private static final String D_CALCULATION = "calculation";
	private static final String D_OBSERVATION = "observation";
	private static final String D_SUM_SCORE = "sum_score";

	public Databasehelper(Context context) {

		super(context, "login_tb", null, 1);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table tb_user(_id integer primary key,name text,psw text,head integer,imei text,speed integer,memory integer,accuracy integer,judgement integer,calculation integer,observation integer,sum_score integer)");

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	/*
	 * 查找数据库里面是否有注册里面的账号密码
	 * 
	 * @param database:数据库
	 * 
	 * @param accountnumber: 登陆的时候查找数据库里面是否有这个账号
	 * 
	 * @param cipher: 登陆的时候查找数据库里面是否有这个密码
	 * 
	 * @param context: 上下文
	 */

	public static void Preserve(SQLiteDatabase database, String accountnumber,
			String cipher, Context context) {

		Cursor cursor = database.query(DATABASE_TABLE, null, D_NAME + "=?",
				new String[] { accountnumber }, null, null, null);
		Cursor cursor1 = database.query(DATABASE_TABLE, null, D_PASS + "=?",
				new String[] { cipher }, null, null, null);
		cursor.close();
		cursor1.close();
	}

	/*
	 * 把账号密码添加到数据库里面
	 * 
	 * @param accountnumber:注册的时候传过来的账号，把这个账号添加到数据库
	 * 
	 * @param cipher: 注册的时候传过来的密码，把这个密码添加到数据库
	 */
	public static void Data(SQLiteDatabase database, String accountnumber, String cipher,Context context) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(D_NAME, accountnumber);
		contentValues.put(D_PASS + "", cipher);
		database.insert(DATABASE_TABLE, null, contentValues);
	}

	/*
	 * 获取数据库里面的账号密码
	 * 
	 * @param database:��һ�����ݿ�Ĳ���
	 * 
	 * @param name���������ݿ��˺�
	 * 
	 * @param pass: �������ݿ�����
	 * 
	 * @param context: һ��������
	 */
	public static void Check(SQLiteDatabase database, String landingaccount,
			String loginpassword, Context context) {
		Cursor cursor = database.query(DATABASE_TABLE, null, D_NAME + "=?",
				new String[] { landingaccount }, null, null, null);
		Cursor cursor1 = database.query(DATABASE_TABLE, null, D_PASS + "=?",
				new String[] { landingaccount }, null, null, null);
		cursor.getCount();
		cursor1.getCount();
		cursor.close();
		cursor1.close();
	}

	/*
	 * �ж����ݿ������Ƿ��д��˺�
	 */

	public static Cursor JudgeAccountnumber(SQLiteDatabase database,
			String accountnumber) {
		Cursor cursor = database.query(DATABASE_TABLE, null, D_NAME + "=?",
				new String[] { accountnumber }, null, null, null);
		return cursor;
	}

	/*
	 * �ж��Ƿ������˺�
	 */
	public static Cursor JudgeLandingaccount(SQLiteDatabase database,
			String landingaccount) {
		Cursor cursor = database.query(DATABASE_TABLE, null, D_NAME + "=?",
				new String[] { landingaccount }, null, null, null);
		return cursor;

	}
}
